/*
 * @Author: szx
 * @Date: 2022-03-13 13:35:44
 * @LastEditTime: 2022-03-13 13:38:21
 * @Description:
 * @FilePath: \leetcode\1-99\90\90_2.js
 */
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsetsWithDup = function (nums) {
    nums.sort();
    const n = nums.length;
    const res = [];
    const visited = new Array(n).fill(false);
    const backtrace = (path, index) => {
        if (path.length <= n) res.push(Array.from(path));
        for (let i = index; i < n; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
            visited[i] = true;
            path.push(nums[i]);
            backtrace(path, i + 1);
            path.pop();
            visited[i] = false;
        }
    };
    backtrace([], 0);
    return res;
};
console.log(subsetsWithDup([1, 2, 2]));
